<?php
class messageController extends base {

    public function __construct()
    {
        parent::__construct();//todo 要把default删除,这里测试临时用
    }

    /**
     * 消息列表
     * @return [type] [description]
     */
    public function doList(){

        $category = common::request('category','G',1);//消息类别 1为新的朋友,2为有道帖子,3为群消息
        $mId = common::request('mId','G',0);//消息ID,唯一标志
        $count = common::request('count','G',100);//返回消息数

        $category = intval($category);
        $mId = intval($mId);
        $count = intval($count);

        $rst = $this->load('message')->findMessage($category, $mId, $count);

        foreach ($rst as $key => $value) {
            $this->load('message')->updateMessageByIsRead($value['id']);
        }
        $ss = $this->load('message')->convertMessageContent($category,$rst);
        //$this->result['result'] = $this->load('message')->convertMessageContent($category,$rst);
        $this->result['result'] = $ss;
    }

    /**
     * 删除消息
     * @return [type] [description]
     */
    public function doDelete(){
        //消息类型 1为新的朋友,2为有道帖子,3为群消息
        $mId = common::request('mId','G');
        $mId = intval($mId);
        $rst = $this->load('message')->deleteMessage($mId);
        $this->result['result'] = array(
            "is_suc"=>$rst,
            "mId" => $mId
        );
    }


    /**
     * 设置消息是否推送(Push)
     * @return [type] [description]
     */
    public function doSetPush(){
        $group_receive = common::request('group_receive','P');
        
        if(empty($group_receive)){
            $result = false;
        }else{
            $uid = $this->uid;
            $result = $this->load('user')->setMessagePushSetting($uid, $group_receive);
        }
        $arr = array(
            "success" => $result == true ? 1 : 0 
        );
        $this->result['result'] = $arr;
    }

    /**
     * 获取消息是否推送(Push)
     * @return [type] [description]
     */
    public function doGetPush(){
        $uid = $this->uid;
        $group_receive = $this->load('user')->getMessagePushSetting($uid);
        $arr = array(
            "group_receive" => json_encode($group_receive),
            "is_first" => empty($group_receive) ? true : false
        );

        $this->result['result'] = $arr;
    }

    /**
     * 有道消息列表
     */
    public function dogetNewsList(){
        $id = common::request('id','G',0);
        $offset = common::request('offset','G',20);
        $uid = $this->uid;
        if($id > 0){
            $list = $this->db->getAll("select id,content,time from r_message_record where category = '6' and id < '$id' and target_id  = '$uid' order by id desc limit $offset");
        }else{
            $list = $this->db->getAll("select id,content,time from r_message_record where category = '6' and target_id  = '$uid' order by id desc limit $offset");
        }
        $data = [];
        foreach($list as $item){
            $content = json_decode($item['content'],true);
            $newsId = $content['news_id'];
            $newsInfo = $this->load('news')->getNewsInfo($newsId);
            if(!empty($newsInfo)){
                $item['user_name']   = '热心人';
                $item['user_header'] = 'http://irun.hupu.com/static/bootstrap/img/jog_logo.png';
                if (isset($newsInfo['uid']) && $newsInfo['uid'] > 0) { //增加帖子用户信息
                    $userInfo = $this->load('client')->getUserInfo($newsInfo['uid']);
                    $item['user_name']   = empty($userInfo) ? '热心人' : $userInfo['nickname'];
                    $item['user_header'] =  empty($userInfo) ? "http://irun.hupu.com/static/bootstrap/img/jog_logo.png" : (!isset($userInfo['header']) ? "http://irun.hupu.com/static/bootstrap/img/jog_logo.png" : $userInfo['header']);
                }
                $item['title'] = $newsInfo['title'];
                $item['content'] = isset($newsInfo['short_desc']) ? $newsInfo['short_desc'] : $newsInfo['content'];
                $item['img'] = isset($newsInfo['thumb_img'][0]) ? $newsInfo['thumb_img'][0] : '';
                $item['add_time'] = date("Y-m-d H:i:s",$item['time']);
                $item['news_id'] = $newsId;
                $data[] = $item;
            }

        }
        $this->result['result'] = $data;
    }
}